package com.ruoyi.graduate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.graduate.domain.entity.MsCommunityPost;
import com.ruoyi.graduate.domain.param.MsCommunityPostAddParam;
import com.ruoyi.graduate.domain.param.MsCommunityPostAuditParam;
import com.ruoyi.graduate.domain.param.MsCommunityPostPageParam;
import com.ruoyi.graduate.domain.param.PageBaseParam;
import com.ruoyi.graduate.service.IMsCommunityPostService;
import com.ruoyi.graduate.service.IMsMessageBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * 社区帖子控制接口
 */
@RestController
@RequestMapping("/api/community")
@Api(tags = "社区帖子控制接口")
public class MsCommunityPostController extends BaseController {

    @Autowired
    private IMsCommunityPostService msCommunityPostService;

    @Autowired
    private IMsMessageBoardService msMessageBoardService;


    @ApiOperation("分页获取社区帖子列表")
    @GetMapping
    public TableDataInfo pageList(MsCommunityPostPageParam param){
        PageInfo pageInfo = msCommunityPostService.pageList(param);

        return getDataTable(pageInfo.getList(),pageInfo.getTotal());
    }


    @ApiOperation("查看社区帖子详情")
    @GetMapping("{id}")
    public AjaxResult detail(@PathVariable("id") Integer id) {

        return AjaxResult.success(msCommunityPostService.detail(id));
    }


    @ApiOperation("审核社区帖子")
    @PutMapping
    public AjaxResult edit(@RequestBody MsCommunityPostAuditParam param) {

        return msCommunityPostService.audit(param);
    }


    @ApiOperation("删除社区帖子")
    @DeleteMapping("{id}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(@PathVariable Integer id) {
        boolean b = msCommunityPostService.removeById(id);

        QueryWrapper qw = new QueryWrapper();
        qw.eq("post_id", id);

        msMessageBoardService.remove(qw);

        return b ? AjaxResult.success() : AjaxResult.error();
    }


    @ApiOperation("用户端社区分页查询")
    @GetMapping("/usercommunitypage")
    public TableDataInfo usercommunitypage(PageBaseParam param) {

        PageInfo pageInfo = msCommunityPostService.userPageList(param);
        return getDataTable(pageInfo.getList(),pageInfo.getTotal());
    }


    @ApiOperation("帖子新增")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody MsCommunityPostAddParam param) {
        return AjaxResult.success(msCommunityPostService.add(param));
    }

}
